package com.dolaw.phoneslidebardemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;

import com.dolaw.phoneslidebardemo.bean.FriendAdapter;
import com.dolaw.phoneslidebardemo.bean.User;
import com.dolaw.phoneslidebardemo.utils.SideBar;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private SideBar sideBar;
    private FriendAdapter friendAdapter;
    private List<User> users = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = this.findViewById(R.id.recyclerView);
        sideBar = this.findViewById(R.id.sidebar);
        users.clear();
        initData();
        initView();

    }

    private void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        friendAdapter = new FriendAdapter(users, this);
        recyclerView.setAdapter(friendAdapter);

        sideBar.setOnStrSelectCallBack(new SideBar.ISideBarSelectCallBack() {
            @Override
            public void onSelectStr(int index, String selectStr) {
                for (int i = 0; i < users.size(); i++) {
                    if (selectStr.equalsIgnoreCase(users.get(i).getStart())){
                        recyclerView.scrollToPosition(i);
                        return;
                    }
                }
            }
        });
    }

    private void initData() {
        users.add(new User("阿三",R.drawable.avator));
        users.add(new User("阿四",R.drawable.avator));
        users.add(new User("阿五",R.drawable.avator));
        users.add(new User("丽丽",R.drawable.avator));
        users.add(new User("妈妈",R.drawable.avator));
        users.add(new User("爸爸",R.drawable.avator));
        users.add(new User("宝贝",R.drawable.avator));
        users.add(new User("#xx",R.drawable.avator));
        users.add(new User("虹猫",R.drawable.avator));
        users.add(new User("蓝兔",R.drawable.avator));
        users.add(new User("阿牛",R.drawable.avator));
        users.add(new User("CK",R.drawable.avator));
        users.add(new User("灯虎",R.drawable.avator));
        users.add(new User("尔康",R.drawable.avator));
        users.add(new User("凡哥",R.drawable.avator));
        users.add(new User("Gr",R.drawable.avator));
        users.add(new User("123阿斯顿",R.drawable.avator));
        Collections.sort(users);
    }
}
